<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<title>Gwack Admin</title>
<link href="style.css" rel="stylesheet" type="text/css" />

<?php
$configfile = '/etc/opt/gwack/gwack.xml';
$xml = simplexml_load_file($configfile);
$tarpath = $xml->tarpath;
$offlinepath = $xml->offlinepath;
$localip = $_SERVER['SERVER_ADDR'];
$agentfilepath = '/var/run/gwack/';

//function to get xml data,  the function is called with a name of a domain or po

function getSettings($object)
{
	$configfile='/etc/opt/gwack/gwack.xml';
	$xml = simplexml_load_file($configfile);

	$offlinepath = $xml->offlinepath;
	$tarpath = $xml->tarpath;

	//loop thru the servers, then load xml data and an array for the matching server name
	foreach ($xml->server as $s)
	{
		if ($s->name == $object)
		{
			$gwname = $s->name;
			$ipaddr = $s->ipaddr;
			$basepath = $s->basepath;
			$restorepath = $s->restorepath;
			$type = $s->type;
		
			if ($type == "domain")
				$agent = "MTA";
			else 
				$agent = "POA";

			if (substr($tarpath, -1) == "/")
				$gwtarpath = $tarpath.$gwname;
			else
				$gwtarpath = $tarpath."/".$gwname;
			

			if (substr($offlinepath, -1) == "/")
				$tarrestorepath = $offlinepath."tarrestore/".$gwname;
			else
				$tarrestorepath = $offlinepath."/tarrestore/".$gwname;
			
			$settings = array(
				"gwname" => $gwname,
				"ipaddr" => $ipaddr,
				"basepath" => $basepath,
				"restorepath" => $restorepath,
				"gwrestorepath" => $offlinepath.$gwname,
				"gwtarpath" => $gwtarpath,
				"tarrestorepath" => $tarrestorepath,
				"type" => $type,
				"agent" => $agent
				);

	   // return the array to the caller

	   return $settings;
	   }
	}
}

function getBackups($basepath)

{
	$backuplist = shell_exec("ls -l $basepath | awk '{print $9}'");
	$backupdirs =  preg_split('/[\r\n]+/', $backuplist);

	return $backupdirs;
}

function getTars($gwtarpath)
{

	$tarlist =	shell_exec("ls -l $gwtarpath | grep -v '^d' | awk '{print $9}'");
	$tars =  preg_split('/[\r\n]+/', $tarlist);

	return $tars;
}


//Get the dom or po name from the url link

$name = $_GET[name];
$settings = getSettings($name);
$backlist = getBackups($settings[basepath]);
$tarlist = getTars($settings[gwtarpath]);

?>

</head>
<body>
<div id="container">
<?php include('menu.php');?>
<div id="content">
	<div id="leftBox">
		<div class="contentBox">
			<div class="contentBoxTop">
				<h3>Set New <?php echo $name;?> Restore Area</h3>
			</div>
			<div class="innerContent" id="box1">
				Select either backup directory or tar file to set a new restore area.
			<hr align="left" width="650"/>
			<table class="sTable">
			<tbody>
			<tr><td class="firstCol" width="160">Current Restore Path</td>
			<td class="secondCol"><?php echo $settings[restorepath];?></td>
			<tr><td>Backup Directories</td>
			<td><form name='setrestore' method='get' action='restore.php'>
			<select name='restoredir' style='width:100px;'>
			<?php
				foreach ($backlist as $backdir)
				{
				echo "<option value='".$backdir."'>".$backdir."</option>";
				}
			?>
			</select>
			<input type='hidden' name='basep' value='<?php echo $settings[basepath]; ?>' ></input>
			<input type='hidden' name='name' value='<?php echo $settings[gwname]; ?>' ></input>
			<input type='hidden' name='restore' value='<?php echo $settings[gwrestorepath]; ?>' ></input>
			</td><td>
			<input	type='image' height="25" src="images/restore.jpg"></input>
			</td></tr>
			</form>
			
			<tr><td class="firstCol" >Existing Tar Files</td>
			<td class="secondCol" >
			<form name='restoretar' method='get' action='restoretar.php'>
			<select name='tar' style="width:100px;">
			<?php
				foreach ($tarlist as $tarfile)
				{
				echo "<option value='".$tarfile."'>".$tarfile."</option>";
				}
			?>
			</select>
			<input type='hidden' name='name' value='<?php echo $settings[gwname]; ?>' ></input>
			<input type='hidden' name='restore' value='<?php echo $settings[gwrestorepath]; ?>' ></input>
			<input type='hidden' name='gwtarpath' value='<?php echo $settings[gwtarpath]; ?>' ></input>
			<input type='hidden' name='tarrestorepath' value='<?php echo $settings[tarrestorepath]; ?>' ></input>
			</td>
			<td>			
			<input type='image' height="25" src="images/restore.jpg"></input>
			</td></tr>
			</form>
			</tbody></table>
			<br>
		</div><!-- end innerContent-->



		<div class="contentBoxTop">
				<h3>Status for <?php echo "$name $settings[agent] ";?>  </h3>
			</div>
			<div class="innerContent" id="box1">

		<?php 
			$agentfile = "$agentfilepath$name.txt";

		 if (file_exists($agentfile))
		 {
			$agentip = trim(file_get_contents($agentfile));
			$agentrunning = 1;
		}

		if ($agentrunning == 1)  
		{ 
			echo '<img height="20"src="images/circle_red.png" />&nbsp&nbsp&nbsp'.$settings[agent].' is currently running on the Gwack server for this post office';
			echo '<br>'.$settings[agent].' is using IP Address: '.$agentip.'.';
		
	
			echo '<ul id="sidebarLinks">';
			if ($agentip != $localip) {
				echo '<li><a href="agent.php?act=unload&name='.$name.'&ip='.$agentip.'&type='.$settings[type].'&remip=yes">Unload '.$settings[agent].' and remove IP Address: '.$agentip.'</a></li>';
			}
			 echo '<li><a href="agent.php?act=unload&name='.$name.'&ip='.$agentip.'&type='.$settings[type].'&remip=no">Unload '.$settings[agent].' and keep IP Address: '.$agentip.'</a></li>';
		} 
		else
		{
			echo '<img height="20" src="images/circle_green.png" />&nbsp&nbsp&nbsp'.$settings[agent].' is not running on the Gwack server for this post office.';
			echo '<br>Select one of the options below to run the '.$settings[agent].' against the restore area listed above.'; 
			echo '<p> If selecting the first option below, ensure that the production server is down or not using the configured IP address shown</p>';
			echo '<ul id="sidebarLinks">';

			echo '<li><a href="agent.php?act=load&name='.$name.'&ip='.$settings[ipaddr].'&type='.$settings[type].'&path='.$settings[gwrestorepath].'">Load '.$settings[agent].' using IP Address: <strong>'.$settings[ipaddr].'</strong></a></li>';
			echo '<li><a href="agent.php?act=load&name=utah&ip='.$localip.'&type=domain&path='.$settings[gwrestorepath].'">Load '.$settings[agent].' using Gwack server local IP Address: <strong>'.$localip.'</strong></a></li>';
		echo '</ul>';
		}
		?>

</div><!--end contentBox-->
	</div><!-- end content -->
  <br class="clearFix" />

</div><!-- end container -->


</body>
</html>

<?php include 'footer.html'; ?>
